package Offer35_138;

import java.util.HashMap;

/**
 * 复杂链表的复制
 * @author 23737
 * @time 2021.10.14
 */
public class Test {
    public static void main(String[] args) {

    }
}

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

/**
 * 使用哈希表来存储
 */
class Solution {
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        HashMap<Node, Node> map = new HashMap<>();
        for (Node cur = head; cur != null ; cur = cur.next) {
            map.put(cur,new Node(cur.val));
        }
        for (Node cur = head; cur != null ; cur = cur.next) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
        }
        return map.get(head);
    }
}
